import { login, logout, getInfo, edit, delUser, findUserById } from '@/api/user'
import { getUserToken, setUserToken, removeUserToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getUserToken(),
    name: '',
    avator: '',
    id: '',
    username: '',
    password: '',
    address: '',
    phone: '',
    idCard: '',
    email: '',
    birthday: '',
    isConsultant: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_USER: (state, user) => {
    state.user = user
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avator = avatar
  },
  SET_ID: (userInfo, id) => {
    userInfo.id = id
  },
  SET_USERNAME: (userInfo, username) => {
    userInfo.username = username
  },
  SET_PASSWORD: (userInfo, password) => {
    userInfo.password = password
  },
  SET_PHONENUMBER: (userInfo, phone) => {
    userInfo.phone = phone
  },
  SET_IDCARD: (userInfo, idCard) => {
    userInfo.idCard = idCard
  },
  SET_REALNAME: (userInfo, name) => {
    userInfo.name = name
  },
  SET_ADDRESS: (userInfo, address) => {
    userInfo.address = address
  },
  SET_GENDER: (userInfo, gender) => {
    userInfo.gender = gender
  },
  SET_BIRTHDAY: (userInfo, birthday) => {
    userInfo.birthday = birthday
  },
  SET_EMAIL: (userInfo, email) => {
    userInfo.email = email
  },
  SET_ISCONSULTANT: (userInfo, isConsultant) => {
    userInfo.isConsultant = isConsultant
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username, password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        commit('SET_USER', data)
        setUserToken(data.token)
        // localStorage.setItem('user_token', data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // del user
  delUser({ commit }, id) {
    return new Promise((resolve, reject) => {
      delUser({ id: id, adminId: parseInt(localStorage.getItem('adminId')) }).then(res => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data.isTrue) {
          return reject('登录失效，请重新登录')
        }
        const { id, username, password, name, idCard, phone, address, avatar, gender, birthday, email,
          isConsultant } = data.user
        commit('SET_AVATAR', avatar)
        commit('SET_REALNAME', name)
        commit('SET_ID', id)
        commit('SET_USERNAME', username)
        commit('SET_PASSWORD', password)
        commit('SET_IDCARD', idCard)
        commit('SET_PHONENUMBER', phone)
        commit('SET_ADDRESS', address)
        commit('SET_GENDER', gender)
        commit('SET_BIRTHDAY', birthday)
        commit('SET_EMAIL', email)
        commit('SET_ISCONSULTANT', isConsultant)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user edit
  edit({ commit, state }, userInfo) {
    const { id, username, password, email, phone, address, birthday, gender, avatar } = userInfo
    return new Promise((resolve, reject) => {
      edit({
        id: id,
        username: username,
        password: password,
        email: email,
        phone: phone,
        address: address,
        birthday: birthday,
        gender: gender,
        avatar: avatar
      }).then(res => {
        commit('SET_USERNAME', res.data.user.username)
        commit('SET_PASSWORD', res.data.user.password)
        commit('SET_IDCARD', res.data.user.idCard)
        commit('SET_PHONENUMBER', res.data.user.phone)
        commit('SET_NAME', res.data.user.name)
        commit('SET_AVATAR', res.data.user.avator)
        commit('SET_BIRTHDAY', res.data.user.birthday)
        commit('SET_GENDER', res.data.user.gender)
        commit('SET_EMAIL', res.data.user.email)
        commit('SET_ISCONSULTANT', res.data.user.isConsultant)
        resolve(res.data)
      }).catch(error => {
        reject(error.message)
      })
    })
  },

  // user logout
  logout({ commit, state }, userId) {
    return new Promise((resolve, reject) => {
      logout(userId).then(() => {
        removeUserToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // // user findUserList
  // findUserList({ commit }, userInfo) {
  //   const { username, phone, pageNum } = userInfo
  //   console.log(userInfo)
  //   return new Promise((resolve, reject) => {
  //     findUserList({ username: username, phone: phone, pageIndex: pageNum }).then(res => {
  //       resolve(res.data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // user findUserById
  findUserById({ commit }, id) {
    return new Promise((resolve, reject) => {
      findUserById({ id: id }).then((res) => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeUserToken() // must remove token first
      commit('RESET_STATE')
      resolve()
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
